package org.sippr.rvt2ifc.service.impl;

import org.sippr.rvt2ifc.dao.ProjectMarkDao;
import org.sippr.rvt2ifc.pojo.ProjectMark;
import org.sippr.rvt2ifc.service.ProjectMarkService;
import org.sippr.rvt2ifc.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ProjectMarkImpl implements ProjectMarkService {
    @Autowired
    private ProjectMarkDao projectMarkDao;

    @Override
    public ProjectMark insert(ProjectMark value) {
        value.setUuid(SnowflakeIdGenerator.nextId());
        //title即为标记编码，获取项目所有编码，计算一个数值，作为标记编码
        ProjectMark projectMark = projectMarkDao.selectMaxByProjectUuid(value.getProjectUuid());
        if (projectMark == null) {
            value.setNumber("A0");
        } else {
            Integer number = ExtractNumber(projectMark.getNumber());
            number++;
            value.setNumber("A" + number);
        }
        value.setTitle("一般");
        value.setContent("这是一个标记");
        Integer count = projectMarkDao.insert(value);
        if (count == 1)
            return value;
        return null;
    }

    private Integer ExtractNumber(String input) {
        String number = input.replaceAll("[^0-9]", "");
        return Integer.parseInt(number);

    }

    @Override
    public List<ProjectMark> list(String projectUuid) {
        return projectMarkDao.selectByProjectUuid(projectUuid);
    }

    @Override
    public void updateContent(ProjectMark projectMark) {
        projectMarkDao.updateByPrimaryKey(projectMark);
    }

    @Override
    public void delete(String uuid) {
        projectMarkDao.deleteByUuid(uuid);
    }
}
